package com.bluesmart.mia;

import org.android.agoo.client.BaseConstants;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.umeng.message.UTrack;
import com.umeng.message.UmengBaseIntentService;
import com.umeng.message.entity.UMessage;

public class MyPushIntentService extends UmengBaseIntentService {
	// 如果需要打开Activity，请调用Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)；否则无法打开Activity。

	@Override
	protected void onMessage(Context context, Intent intent) {
		// 需要调用父类的函数，否则无法统计到消息送达
		super.onMessage(context, intent);
		String message = intent.getStringExtra(BaseConstants.MESSAGE_BODY);
		JSONObject obj = JSON.parseObject(message);
		int type = 0;
		try {
			type = obj.getJSONObject("extra").getIntValue("notifyType");
			Log.i("------", message);
		} catch (Exception e) {
		}
		
		getNotification(context,obj.getJSONObject("body"),type);
	}
	
	public void getNotification(Context context,JSONObject obj,int type) {
		NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
        //定义notification  
        Notification n=new Notification(R.drawable.icon,obj.getString("title") + obj.getString("text"),System.currentTimeMillis());  
        n.flags=Notification.FLAG_AUTO_CANCEL;  
        //通知消息与Intent关联  
        Intent it = new Intent(context,MainTabActivity.class);  
        it.putExtra("type", type);  
        PendingIntent pi=PendingIntent.getActivity(context, 100, it, PendingIntent.FLAG_CANCEL_CURRENT);  
        //具体的通知内容  
        n.setLatestEventInfo(context, obj.getString("title"), obj.getString("text"),pi);  
        //执行通知  
        nm.notify(1, n);  
	}
}
